'''
@文件    :views.py
@说明    :
@时间    :2020/04/04 17:05:39
@作者    :didiplus
@版本    :1.0
'''
from .__init__ import *
from flask import Blueprint,make_response,jsonify,json,request
comment = Blueprint('comment',__name__,url_prefix='/api/comment')

types = {
    "0": 'R_SO_4_',   # 歌曲
    "1": 'R_MV_5_',   # M V
    "2": 'A_PL_0_',   # 歌单
    "3": 'R_AL_3_',   # 专辑
    "4": 'A_DJ_1_',   # 电台
    "5": 'R_VI_62_',  # 视频
    "6": 'A_EV_2_'    # 动态
}

def pub(request, url, cookies={"os": "pc"}):
    query = request_query(request,
                          ["id", "rid"],
                          ["limit", {"limit": 20}],
                          ["offset", {"offset": 0}],
                          ["before", {"beforeTime": 0}])
    data = Send(query).POST("weapi/v1/resource/" +
                            url + query["rid"], cookies)
    return data.text


# 歌曲评论
@comment.route('/song',methods=['POST','GET'])
def song():
    return make_response(jsonify(json.loads(pub(request,"comments/R_SO_4_"))),403)

# 专辑评论
@comment.route('/album',methods=['POST','GET']) 
def album():
    return make_response(jsonify(json.loads(pub(request, "comments/R_AL_3_"))),403)


# 歌单评论
@comment.route('/playlist',methods=['POST','GET'])
def playlist():
    return make_response(jsonify(loads(pub(request, "comments/A_PL_0_"))),403)

# 电台评论
@comment.route('/dj',methods=['POST','GET'])
def dj():
    return make_response(jsonify(loads(pub(request, "comments/A_DJ_1_"))),403)

# 视频评论
@comment.route('/video',methods=['POST','GET'])
def video():
    return make_response(jsonify(loads(pub(request, "comments/R_VI_62_"))),403)

# 视频评论
@comment.route('/mv',methods=['POST','GET'])
def mv():
    return make_response(jsonify(loads(pub(request, "comments/R_MV_5_"))),403)

 # 热门评论
@comment.route('/hot',methods=['POST','GET'])
def hot():
    query = request_query(request, "type")
    return make_response(jsonify(loads(pub(request, "hotcomments/"+types[query["type"]]))),403)

# 动态评论
@comment.route('/event',methods=['POST','GET'])
def event():
    
    query = request_query(request, "id",
                          ["limit", {"limit": 20}],
                          ["offset", {"offset": 0}],
                          ["before", {"beforeTime": 0}])
    id = query.pop("id")
    data = Send(query).POST("weapi/v1/resource/comments/" + id)
    return make_response(jsonify(json.loads(data.text)),403)

# 点赞/取消点赞评论
@comment.route('/like',methods=['POST','GET'])
def like():
    query = request_query(request, "id", "type", [
                          "t", {"t": 1}], ["cid", "commentId"])
    t = "like" if query.pop("t") == "1" else "unlike"
    tp = types[query.pop("type")]
    id = query.pop("id")
    query["threadId"] = id if tp == "A_EV_2_" else tp + id
    data = Send(query).POST("weapi/v1/comment/" + t, {"os": "pc"})
    return make_response(jsonify(loads(data.text)),403)

